import os
from datetime import datetime
from src.custom.tools import read_json, write_json, calculate_hash

static_path = os.path.join("..", "static")
static_file_cache_path = os.path.join("cache", "file_cache")
file_manager_config_path = os.path.join("config", "file_cache_config.json")
def get_file_cache_dict(file: str | bytes) -> dict | str:
    """
    获取文件缓存信息。

    字典格式：

    d = {
        "file_name": file_name,             \n
        "file_path": file_path,             \n
        "file_text_path": file_text_path,   \n
        "update_time": formatted_now,       \n
    }
    :param file: 要读取的文件，可以是一个路径，或者二进制
    :return: 返回一个缓存的字典信息或者一个计算好的hash值
    """
    # TODO 可能要改
    cache_config = os.path.join(".", file_manager_config_path)
    hash_sha256 = calculate_hash(file)
    config = read_json(cache_config)
    if config is not None and hash_sha256 in config:
        return config[hash_sha256]
    return hash_sha256

def get_file_cache_dict_by_(file_hash: str) -> dict | None:
    """
    获取文件缓存信息。

    字典格式：

    d = {
        "file_name": file_name,             \n
        "file_path": file_path,             \n
        "file_text_path": file_text_path,   \n
        "update_time": formatted_now,       \n
    }
    :param file_hash: 要读取的文件hash值
    :return: 返回一个缓存的字典信息或者None
    """
    # TODO 可能要改
    cache_config = os.path.join(".", file_manager_config_path)
    config = read_json(cache_config)
    if config is not None and file_hash in config:
        return config[file_hash]
    return None

def write_file_cache(file_name: str, file: str | bytes, file_text: list, sha256: str) -> dict:
    # TODO 可能要改
    file_storage_dir = os.path.join(static_path, static_file_cache_path)
    cache_config = os.path.join(file_manager_config_path)
    config = read_json(cache_config)
    config = config if config is not None else {}

    # 分离文件名和扩展名
    name, ext = os.path.splitext(file_name)
    # 将sha256拼接在文件名后面，扩展名前面
    file_name = f"{name}_{sha256}{ext}"
    file_path = os.path.join(file_storage_dir, file_name)
    file_text_path = os.path.join(file_storage_dir, file_name+"_text.json")
    os.makedirs(file_storage_dir, exist_ok=True)

    # 写二进制
    if type(file) is str:
        # 如果是路径的话，先读取，再写入
        with open(file, "rb+") as file_object:
            file = file_object.read()
    with open(file_path, "wb+") as file_object:
        file_object.write(file)

    # 写txt json
    write_json(file_text_path, file_text)

    # 获取当前时间
    current_date = datetime.now()
    formatted_now = current_date.strftime("%Y-%m-%d")
    d = {
        "file_name": file_name,
        "file_path": file_path,
        "file_text_path": file_text_path,
        "file_hash": sha256,
        "update_time": formatted_now,
    }
    config[sha256] = d
    write_json(cache_config, config)
    return d




if __name__ == '__main__':
    file_path = ""
    h = get_file_cache_dict(file_path)
    print(h)
    if type(h) is str:
        write_file_cache("安装驱动.docx", file_path, {"text":"这是文件的内容","coords":[{"a":"a"},{"b":"b"}]}, sha256=h)
    else:
        print("is not str")